import { getRoleListApi } from "@/api/role/role";
import { RoleListParm } from "@/api/role/RoleModel";
import { onMounted, reactive } from "vue";
import { getUserId } from "@/utils/auth";
// 操作用户模块表格的文件
export default function useRoleTable() {
  // 表格的数据
  const roleTable = reactive({
    list: []
  })
  // 表格的查询参数
  const listParm = reactive<RoleListParm>({
    userId: getUserId() || '',
    currentPage: 1,
    pageSize: 10,
    name: '',
    total: 0
  })
  // 获取表格的数据
  const getRoleList = async () => {
    let res = await getRoleListApi(listParm)
    if (res && res.code == 200) {
      console.log('表格数据')
      console.log(res)
      roleTable.list = res.data.records
      listParm.total = res.data.total;
    }
  }
  //搜索按钮
  const searchBtn = () => {
    getRoleList()
  }
  //重置按钮
  const resetBtn = () => {
    listParm.name = '';
    getRoleList()
  }
  //页容量改变触发
  const sizeChange = (size: number) => {
    listParm.pageSize = size;
    getRoleList();
  }
  //页数改变触发
  const currentChange = (page: number) => {
    listParm.currentPage = page;
    getRoleList();
  }
  onMounted(() => {
    getRoleList()
  })
  return {
    roleTable,
    listParm,
    getRoleList,
    searchBtn,
    resetBtn,
    sizeChange,
    currentChange
  }
}